﻿Library.UserInterface.Widgets.All = Array();

Library.UserInterface.Widgets.Add = function (object) {
    Library.Array.Add(Library.UserInterface.Widgets.All, object);    
}

Library.UserInterface.Widgets.Get = function (id) {
    for (var i = 0; i < Library.UserInterface.Widgets.All.length; i++) {
        if (Library.UserInterface.Widgets.All[i].id == id) {
            return Library.UserInterface.Widgets.All[i];
        }
    }
}

Library.UserInterface.Widgets.Dispose = function (id) {
    var widget = Library.UserInterface.Widgets.Get(id);
    widget.container.find(".ui-widget-error").remove();
    Library.Array.RemoveByObject(Library.UserInterface.Widgets.All, widget);
}

Library.UserInterface.Widgets.Error = function (id, error) {
    var widget = Library.UserInterface.Widgets.Get(id);
    var div = $("<div class=\"ui-widget-error\" style=\"position:absolute; left:" + (widget.container.position().left - 32) + "; top:" + widget.container.position().top + "\"><img src=\"/Javascript/Widgets/widget-error.png\" alt=\"\" style=\"cursor:pointer\" onclick=\"Library.UserInterface.Widgets.Refresh('" + id + "')\"/></div>");
    div.prependTo(widget.container);
    
    if (window.console && window.console.error)
        console.error(error);
}

Library.UserInterface.Widgets.Refresh = function (id) {
    var widget = Library.UserInterface.Widgets.Get(id);
    if (widget)
        widget.initialize(false);
}

Library.UserInterface.Widgets.SetHeaderIcons = function (widget) {
    if (!widget)
        return;

    if (widget.fullscreen) {
        if (widget.container.find(".portlet-header").find(".ui-icon-newwin").length == 0) {
            widget.container.find(".portlet-header").prepend("<span class='ui-icon ui-icon-newwin TipTip' title='Tam Ekran'></span>").end();
            widget.container.find(".portlet-header").find(".ui-icon-newwin").bind('click', function () {
                if (widget.container.hasClass("portletFullScr")) {
                    widget.container.removeClass("portletFullScr");
                    widget.container.find(".portlet-content").css("max-height", "200px");
                } else {
                    widget.container.addClass("portletFullScr");
                    widget.container.find(".portlet-content").css("max-height", "100%");
                }
                widget.initialize(false);
            }).end();
        }
    }

    if (widget.settings) {
        if (widget.container.find(".portlet-header").find(".ui-icon-wrench").length == 0) {
            widget.container.find(".portlet-header").prepend("<span class='ui-icon ui-icon-wrench TipTip' title='Özelleştir'></span>").end();
            widget.container.find(".portlet-header").find(".ui-icon-wrench").bind('click', function () {
                widget.container.find(".portlet-widget").toggle('blind', {}, 500);
                widget.container.find(".portlet-settings").toggle('blind', {}, 500);
            }).end();
        }
    }

    if (widget.minimize) {
        if (widget.container.find(".portlet-header").find(".ui-icon-minusthick").length == 0) {
            widget.container.find(".portlet-header").prepend("<span class='ui-icon ui-icon-minusthick TipTip' title='Daralt'></span>").end();
            widget.container.find(".portlet-header").find(".ui-icon-minusthick").bind('click', function () {
                $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
                $(this).parents(".portlet:first").find(".portlet-content").toggle("slow");
            });

            widget.container.find(".portlet-header").find(".ui-icon-plusthick").bind('click', function () {
                $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
                $(this).parents(".portlet:first").find(".portlet-content").toggle("slow");
            });
        }
    }
}